package com.tianyang.yukuaifu.business.main.presenter;

import com.tianyang.yukuaifu.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifu.business.main.contract.ChooseSoundContract;
import com.tianyang.yukuaifu.business.main.model.MainModel;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：
 * Created by xyj on 2024/4/24
 */
public class ChooseSoundPresenter implements ChooseSoundContract.Presenter {

    private ChooseSoundContract.View mView;

    private CompositeSubscription mSubscription;

    public ChooseSoundPresenter(ChooseSoundContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getDeviceList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getDeviceList("VOICE_BOX")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<DeviceOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showDeviceListView(null);
                    }

                    @Override
                    public void onNext(List<DeviceOutput> outputs) {
                        mView.showDeviceListView(outputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
